import React, { Component } from 'react';
import OrderSearch from './OrderSearch';
import OrderTable from './OrderTable';
import api from '../../../api/api';
import './index.scss';
class HisOrder extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: [],
      searchData: {},
      pageSize:10,
      pageIndex:1
    };
  }
  componentWillMount() {
    this.searchData();
  }
  searchData = async (data,submitClick) => {
    if(submitClick){
      this.setState({
        searchData:data
      })
    }else{
      data = this.state.searchData
    }
    const pageIndex = submitClick ? 1:this.state.pageIndex
    const pageSize = this.state.pageSize
    const skip = pageSize * (pageIndex - 1);
    const limit = pageSize;
    let tableData  = {}
    tableData.list = await api.getSaleOrder(data, skip, limit + 1);

    if (tableData.list) {
        if (tableData.list.length > limit) {
            tableData.count = skip + limit + 1;
            tableData.list = tableData.list.slice(0, limit);
        } else {
            tableData.count = skip + (tableData.list ? tableData.list.length : 0);
        }
    } else {//兼容不存在list属性的数据结构
        tableData.count = skip + (tableData.length ? tableData.length : 0);
        tableData.list = Array.isArray(tableData) && tableData.slice(0, limit);
    }
    tableData.pageIndex = Number(submitClick ? 1:this.state.pageIndex)
    tableData.pageSize = Number(this.state.pageSize)
    this.setState({
      tableData: tableData,
    });
  };
  pageChange=(pagination)=>{
    // console.log('pagination:',pagination)
    const pageSize = pagination.pageSize;
    const pageIndex = pagination.current;
    this.setState({
      pageSize:pageSize,
      pageIndex:pageIndex
    })
    setTimeout(()=>{
      this.searchData({})
    },0)
  }
  getTableChildMsg = () => {
    console.log('接收到了从table穿过来的事件了')
    this.searchData()
  }


  render() {
    return (
      <div>
        <OrderSearch searchData={this.searchData} ref="childrenSearch"></OrderSearch>
        <OrderTable
          tableData={this.state.tableData}
          pageChange={this.pageChange}
          ref="childrenTable"
          parent={ this }
        ></OrderTable>
      </div>
    );
  }
}

export default HisOrder;
